//
//  GGBouncedBaseView.m
//  unzip
//
//  Created by yg on 2021/11/21.
//

#import "GGBouncedBaseView.h"

@implementation GGBouncedBaseView

- (id)init{
	if (self = [super init]) {
		
		_headerHeight = 30;
		_contentHeight = 300;
		_backgroundAlpha = 0.3;
		_boxCornerRadius = 20;
		_boxBottomOffset = 0;
		if (@available(iOS 11.0, *)) {
			_contentBottomOffset = [UIApplication sharedApplication].windows.firstObject.safeAreaInsets.bottom;
		}
		
		self.userInteractionEnabled = YES;
		_backgroundButton = [UIButton buttonWithType:UIButtonTypeCustom];
		_backgroundButton.backgroundColor = [UIColor blackColor];
		[_backgroundButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchDown];
		[self addSubview:_backgroundButton];
		
		
		_boxView = [[UIView alloc] init];
		_boxView.userInteractionEnabled = YES;
		_boxView.layer.cornerRadius = _boxCornerRadius;
		_boxView.backgroundColor = [UIColor whiteColor];
		_boxView.clipsToBounds = NO;
		_boxView.layer.shadowColor = GGColor(blackColor).CGColor;
		_boxView.layer.shadowOffset = CGSizeMake(1, 1);
		_boxView.layer.shadowOpacity = 0.3;
		_boxView.layer.shadowRadius = 10;
		[self addSubview:_boxView];
		
		_headerView = [[UIView alloc] init];
		_headerView.userInteractionEnabled = YES;
//		_headerView.backgroundColor = [UIColor redColor];
		[_boxView addSubview:_headerView];
		
		_contentView = [[UIView alloc] init];
		_contentView.userInteractionEnabled = YES;
//		_contentView.backgroundColor = [UIColor orangeColor];
		[_boxView addSubview:_contentView];
		
	}
	return self;
}

- (void)layoutSubviews{
	[super layoutSubviews];
	[self layoutItem];
}

- (void)layoutItem{
	
	
	CGFloat headerHeight = _headerHeight;
	CGFloat contentHeight = _contentHeight;
	CGFloat boxHeight = [self boxHeight];
	CGFloat viewWidth = self.superview.bounds.size.width;
	CGFloat viewHeight = 0;
	
	if (_isBackground) {
		viewHeight = self.superview.frame.size.height;
		self.frame = self.superview.bounds;
		_backgroundButton.frame = self.bounds;
		_backgroundButton.hidden = NO;
	}else{
		viewHeight = [self boxHeight];
		self.frame = CGRectMake(0, self.superview.bounds.size.height - viewHeight, viewWidth, viewHeight);
		_backgroundButton.frame = self.bounds;
		_backgroundButton.hidden = YES;
	}
	_boxView.frame = CGRectMake(0, viewHeight - boxHeight - _boxBottomOffset, viewWidth, boxHeight);
	_headerView.frame = CGRectMake(0, 0, viewWidth, headerHeight);
	_contentView.frame = CGRectMake(0, headerHeight, viewWidth, contentHeight);
	
}

- (CGFloat)boxHeight{
	return _headerHeight + _contentHeight + _contentBottomOffset;
}

- (CGFloat)boxTop{
	return _headerHeight + _contentHeight + _boxBottomOffset + _contentBottomOffset;
}

- (void)showInView:(UIView *)view{
	_backgroundButton.alpha = _backgroundAlpha;
	[view addSubview:self];
	
	[self layoutItem];
	_backgroundButton.alpha = 0;
	CGRect rect = self.boxView.frame;
	rect.origin.y = rect.origin.y + [self boxTop];
	self.boxView.frame = rect;
	[UIView animateWithDuration:0.3 animations:^{
		self.backgroundButton.alpha = self.backgroundAlpha;
		[self layoutItem];
	} completion:^(BOOL finished) {
		
	}];
	
}

- (void)dismiss{
	[UIView animateWithDuration:0.3 animations:^{
		self.backgroundButton.alpha = 0;
		CGRect rect = self.boxView.frame;
		rect.origin.y = rect.origin.y + [self boxTop];
		self.boxView.frame = rect;
	} completion:^(BOOL finished) {
		[self removeFromSuperview];
	}];
}

@end
